/* Turska Framework/UI library (Turs2)
 * Copyright 2003-2010 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/turska/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: T2VerticalStripBitmapFont.h 157 2010-01-01 23:31:43Z jlauha $
 * $Revision: 157 $
 */

#ifndef T2VERTICALSTRIPBITMAPFONT_H_INCLUDED
#define T2VERTICALSTRIPBITMAPFONT_H_INCLUDED


namespace turska
{
    //! Simple bitmap font class which loads glyphs from a vertical strip.
    /*! The vertical strip must contain characters in the order they appear
     * in a given font string. Each glyph is assumed to be in a square region,
     * where side length of the square equals width of the image. A font using
     * the default font string has 95 characters, so its height should equal
     * 95*width pixels (so image size could be e.g. 30x2850 pixels).
     * Width of each glyph is determined load-time by tracing alpha channel.
     *
     * \note The current implementation is unoptimized. The font image is split
     *       to one Graphics Image (texture) per glyph. For hardware
     *       accelerated rendering it is not optimal to change active texture
     *       for each glyph.
     */
    class VerticalStripBitmapFont : public Font
    {
    public:
        //! Defines vertical strip bitmap font related constants.
        enum VSBF_CONSTANTS
        {
            //! Maximum amount of glyphs.
            /*! \note Current implementations assume 8 bits per character.
             */
            MAX_GLYPHS = 256,
            MAX_FILENAME_LENGTH = 256
        };

        //! Constructor caches the file name and builds glyph map using font string.
        /*! \note Default font string is
         *        " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".
         * \param graphics Graphics context to use for drawing.
         * \param fileName image file to load the font from.
         * \param fontString ordered string of characters appearing in the font image.
         * \param alphaTraceMinAlpha minimum alpha value to be traced as a visible pixel.
         *                           Ignored if the image surface lacks alpha.
         */
        VerticalStripBitmapFont(Graphics *graphics, const C8 *fileName,
                                const C8 *fontString = 0, UI8 alphaTraceMinAlpha = 16);
        virtual ~VerticalStripBitmapFont();

        virtual bool init();

        virtual void setCharacterSpacing(I32 spacing) { mCharacterSpacing = spacing; }
        virtual I32 getCharacterSpacing() { return mCharacterSpacing; }
        virtual I32 getFontHeight() { return mFontHeight; }
        virtual TREAL getFontHeight(TREAL scale) { return mFontHeight * scale; }
        virtual bool hasCharacter(UI32 character);

        virtual bool reload() = 0;

        virtual TREAL getStringWidth(const C8 *string, TREAL scale, I32 substrLength = -1);

        virtual TREAL drawString(TREAL x, TREAL y, const C8 *string,
                                UI32 flags = IF_ALIGN_TOP_LEFT, TREAL scale = 1,
                                const Color &color = Color(0xffffffff), I32 substrLength = -1);

    protected:

        void buildCharMap(const C8 *fontString);

        Graphics *mGraphics;
        UI8 mAlphaTraceMinAlpha;
        C8 mFileName[MAX_FILENAME_LENGTH];
        I16 mGlyphMap[MAX_GLYPHS];
        Image **mGlyphs;
        UI32 mGlyphCount;
        I32 mCharacterSpacing;
        UI32 mFontHeight;

    }; // class Font

} // namespace turska


#endif // !T2VERTICALSTRIPBITMAPFONT_H_INCLUDED
